package com.cqc.elevationbasic01;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private boolean flag = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View view_blue = findViewById(R.id.view_blue);
        final View view_red = findViewById(R.id.view_red);

        //点击后让红色按钮移到上方
        view_red.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG, "点击了红色按钮");
                if (flag) {
                    view.setTranslationZ(0);//Api>=21
                    flag = false;
                } else {
                    view.setTranslationZ(20);//Api>=21
                    flag = true;
                }
            }
        });

        //返回false表示不处理触摸事件，那么点击事件setOnClickListener(...)才有效
        view_red.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        view_red.setElevation(100);
//                        view_red.setTranslationZ(50);
                        break;
                    case MotionEvent.ACTION_UP:
                        view_red.setElevation(0);
//                        view_red.setTranslationZ(0);
                        break;
                }
                return false;
            }
        });
    }
}
